/**
* eCommerce Application Sample for J2EE Training 
* Implementation of CustomerBean
* @author Fabienne Boyer - Didier Donsez
* may 2006
*/

package ecom.beans;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.NamedQuery;
import javax.persistence.OneToOne;
import javax.persistence.Table;

@Entity
@NamedQuery(name = "AllCustomers", query = "select c from CustomerBean c")
public class CustomerBean implements java.io.Serializable{

	/**
	 * Customer Bean UID (for serialization)
	 */
	private static final long serialVersionUID = 2024630002065501844L;

	//Customer Identifier (Primary key)
	//@Id
	//int accountId;
    
	//Customer's account id
	//int accountId;
	//Customer's AccountBean reference
	@OneToOne(optional=true)
	AccountBean account;
	
	//Password, MD5-encrypted
	byte[] pwd;
	
	//Name
	String nom;
	
	//Surname
	String prenom;
	
	//Adresse
	String adresse;
	
	//Code Postal
	int codePostal;
	
	//Pays
	String country;
	
	//Tel
	String tel;
	

	//Email
	@Id
	String email;
	
	
	public CustomerBean(){
	}

	/**
	 * CustomerBean constructor
	 * @param customerId customer identifier
	 * @param accountId customer's account
	 * @param pwd customer's password
	 * @param nom customer's name
	 * @param prenom customer's surname
	 * @param adresse customer's address
	 * @param codePostal customer's postal code
	 * @param country customer's country
	 * @param tel customer's phone number
	 */
	public CustomerBean(byte[] pwd, String nom,
			String prenom, String adresse, int codePostal, String country,
			String tel, String email) {
		super();
		account = null;
		this.pwd = pwd;
		this.nom = nom;
		this.prenom = prenom;
		this.adresse = adresse;
		this.codePostal = codePostal;
		this.country = country;
		this.tel = tel;
		this.email = email;
	}

	//GETTERS AND SETTERS

	public AccountBean getAccount() {
		return this.account;
	}
	
	public void setAccount(AccountBean account) {
		this.account = account;
	}

	public byte[] getPwd() {
		return pwd;
	}

	public void setPwd(byte[] pwd) throws NoSuchAlgorithmException {
		
		MessageDigest md = MessageDigest.getInstance("MD5");
		md.update(pwd);
		byte[] pwd1 = md.digest();
		this.pwd = pwd1;
	}

	public String getNom() {
		return nom;
	}

	public void setNom(String nom) {
		this.nom = nom;
	}

	public String getPrenom() {
		return prenom;
	}

	public void setPrenom(String prenom) {
		this.prenom = prenom;
	}

	public String getAdresse() {
		return adresse;
	}

	public void setAdresse(String adresse) {
		this.adresse = adresse;
	}

	public int getCodePostal() {
		return codePostal;
	}

	public void setCodePostal(int codePostal) {
		this.codePostal = codePostal;
	}

	public String getCountry() {
		return country;
	}

	public void setCountry(String country) {
		this.country = country;
	}

	public String getTel() {
		return tel;
	}

	public void setTel(String tel) {
		this.tel = tel;
	}
	

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}
	
	public String toString() {
		return " Customer : " + nom + " , " + prenom +" , " +  adresse;
}
	
}
